<%@ page language="java" import="java.util.*,com.wonders.frame.chron.util.StringUtil" pageEncoding="UTF-8"%>
<%@ include file="/jsp/include/taglib.jsp"%>
<%@ include file="/jsp/include/headform.jsp"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

String modelName = StringUtil.getNotNullValueString(request.getParameter("modelName"));
String modelId = StringUtil.getNotNullValueString(request.getParameter("modelId"));
String groupName = StringUtil.getNotNullValueString(request.getParameter("groupName"));
String targetType = StringUtil.getNotNullValueString(request.getParameter("targetType"));
String procType = StringUtil.getNotNullValueString(request.getParameter("procType"));

String attachId = StringUtil.getNotNullValueString(request.getParameter("attachId"));

if("".equals(modelId)){
	modelId = String.valueOf(System.currentTimeMillis());
}

%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>上传附件</title>
				
<script type="text/javascript">

var hidden = null;
<%
if(!"".equals(attachId)){
	if ("open".equals(targetType)) { //按弹出页面处理%>
	try {
		hidden = window.opener.document.getElementById("<%=attachId%>");
	} catch (e) {}
	<%} else if (targetType.equalsIgnoreCase("frame")) { //按iframe处理%>
	try {
		hidden = parent.document.getElementById("<%=attachId%>");
	} catch (e) {}
<%
	}
}%>

$(function(){
	getFileInfo();
	
	$("#uploadify").uploadify({
		'auto' : false,
	       'method' : "post",
	       'height' : 20,
	       'width' : 100,
	       'swf' : '${ctxStatic}/uploadify/uploadify.swf', 
	       'uploader' : '${ctx}/api/attach/upload?modelName=<%=modelName%>&modelId=<%=modelId%>&groupName=<%=groupName%>',
	       'fileTypeDesc' : '格式:txt,xls，xlsx,doc,docx,rar,zip,jpg,png,pdf',		//描述
	       'fileTypeExts' : '*.txt;*.xls;*.xlsx;*.doc;*.docx;*.rar;*.zip;*.jpg;*.png;*.pdf',			//文件类型
	       'fileSizeLimit' : '20000KB',			//文件大小
	       'buttonText' : '选择文件',			//按钮名称
	       'fileObjName'	:'uploadify',
	       'multi' :true,
	       'successTimeout' : 5,
	       'requeueErrors' : false,
	       'removeTimeout' : 1,
	       'removeCompleted' : true,
	  		'onSelect' : function(file) {
	  			$(".uploadify").css("float","");
	     	},	       
       'onSelectError' : function(file,errorCode,errorMsg) {
       		if(errorCode==-110){
       			this.queueData.errorMsg = "文件太大，无法上传！";
       		}
        }, 
       'onUploadStart' : function(file) {
            $("#uploadify").uploadify("settings", "formData",{'attachMemo' : '附件1'});
        },
        'onUploadSuccess' : function(file, data, response){
		      var result = jQuery.parseJSON(data);
		      var attach=result.data[0];
		      
	       		$("#fileTable").show();
	       		var addHtml = buildHtml(attach);

	       		if(hidden != null){
	       			if(hidden.value==''){
	       				hidden.value = attach.id;
	       			}else{
	       				hidden.value += ','+attach.id;
	       			}
	       		}
	       		$("#fileBody").append(addHtml);
	       		$("#fileBody").show();
    	}
	});
	
	$(".uploadify").css("float","left");
});

function buildHtml(attach){
	
	var addHtml = "<tr style='word-break:break-all; word-wrap:break-word;'>"+
		"<td class='t_l' style='width:200px;'>"+
			"<a href='${ctx}/api/attach/download/"+attach.id+"'>"+attach.fileName+"</a>"+
		"</td>"+
		"<td class='t_c'>"+attach.fileSize+"</td>"+
		"<td class='t_c'>"+FormatDate(attach.uploadDate)+"</td>"+
		"<td class='t_c'><a href='${ctx}/api/attach/download/"+attach.id+"'>下载</a></td>";
		;
		
	if('<%=procType%>' == 'edit'){
		addHtml += "<td class='t_c'><a href='#' id="+attach.id+" name='attach_id' onclick='removeFile(this); return false;'>删除</a></td>";	
	}
		
	addHtml += "</tr>";
	return addHtml;
}

function FormatDate(strTime) {
    var date = new Date(strTime);
    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
}

function checkForm(){
	// 	检查第一个附件表单
	var attachIds ="";
	$("#fileBody").find("a[name=attach_id]").each(function(){
		attachIds += $(this).attr("id")+",";
	});
	
	if(attachIds!=null && attachIds!="" && attachIds.length>1){
		attachIds = attachIds.substring(0,attachIds.length-1);
	}
	$("#attachIds").val(attachIds);
	
	return true;
}

//删除上传的附件
function removeFile(paramObj){
	if(confirm("是否删除，删除后无法恢复")){	
		var $target = $(paramObj);
		$.ajax({
			url			: 	"<%=basePath%>/api/attach/del/"+$target.attr("id"),
			async: false,
			dataType	: 	"json",
			type		:	'delete',
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
// 				alert("系统连接失败请稍后再试！");
			},
			success: function( data ) {
				$target.parent().parent().remove();
				var trLength = $("#fileTable").find("tr").length;
				if(trLength==1){
					$("#fileTable").hide();
				}
			}
		});
	}
}

function getFileInfo(){
        $.ajax({
                url : "<%=basePath%>api/attach?modelName=<%=modelName%>&modelId=<%=modelId%>&groupName=<%=groupName%>",
                type : 'get',
                dataType:'json',
                success: function(data, status, xhr) {
            		      var attachs=data.data;
            		      
            		      for(var i = 0;i < attachs.length;i++){
            		    	  var attach = attachs[i];
	              	       		var addHtml = buildHtml(attach)	  
    			       		$("#fileBody").append(addHtml);     
            		      }
                }
        });
}
</script>
<style>
font {font-family: 黑体;font-size:9pt;}
table, td, th  
{  
	border:1px solid #eeeeee;
}
</style>
</head>

<body style="width: 100%; background-color: transparent; background-color: FFF;">
	<div style="width: 98%;">
		<%if("edit".equals(procType)){%>
		<input type="file" name="uploadify" id="uploadify" />
<!-- 		<span style='margin-left:5px;font-size:9pt;display:inline;'>附件说明:</span>

		<select name="attachMemo" id="attachMemo">
			<option value="正文">正文</option>
			<option value="正文之附件">正文之附件</option>					
			<option value="第三方证明">第三方证明</option>
			<option value="评审类意见">评审类意见</option>
			<option value="其它材料">其它材料</option>
		</select>
		-->
		&nbsp;<input type="button" value="上传" onclick="$('#uploadify').uploadify('upload');">
		 
		 &nbsp;&nbsp;单个附件不允许超过20M
		<%} %>
		<table width="100%" style="margin:3px;">
			<tbody id="fileBody">
				<tr>
					<td width="200px;" class="t_c">文件名</td>
					<td width="100px;" class="t_c">大小(KB)</td>
					<td width="150px;" class="t_c">上传时间</td>
					<td width="100px;" class="t_c" colspan="2">操作</td>
				</tr>
			</tbody>
		</table>

	</div>
</body>

</html>
